package aishua.com.mytestapplist.adapter;

import android.content.Context;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import aishua.com.mytestapplist.R;
import aishua.com.mytestapplist.bean.ItemBean;

/**
 * Created by aishua on 2018/3/27.
 */

public class MyListViewAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<ItemBean> dataList;

    public MyListViewAdapter(Context mContext,ArrayList dataList){

        this.mContext = mContext;
        this.dataList = dataList;
    }
    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {

        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        View view = null;
        if (convertView ==null){
            LayoutInflater factory = LayoutInflater.from(mContext);
             view =factory.inflate(R.layout.itemview_item,null);
             viewHolder = new ViewHolder();
             viewHolder.fruit_name = (TextView) view.findViewById(R.id.fruit_name);
             viewHolder.fruit_img = (ImageView) view.findViewById(R.id.fruit_image);
             view.setTag(viewHolder);
        }else {
              view = convertView;
              viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.fruit_img.setBackgroundResource(dataList.get(position).getFruitImg());
        viewHolder.fruit_name.setText(dataList.get(position).getFruitName());
        return view;
    }

    public class ViewHolder{

        TextView fruit_name;
        ImageView fruit_img;
    }
}
